package org.apache.harmony.javax.security.auth;

import java.security.DomainCombiner;
import java.security.Principal;
import java.security.ProtectionDomain;
import java.util.Set;

public class SubjectDomainCombiner
  implements DomainCombiner
{
  private static final AuthPermission _GET = new AuthPermission("getSubjectFromDomainCombiner");
  private Subject subject;

  public SubjectDomainCombiner(Subject paramSubject)
  {
    if (paramSubject == null)
      throw new NullPointerException();
    this.subject = paramSubject;
  }

  public ProtectionDomain[] combine(ProtectionDomain[] paramArrayOfProtectionDomain1, ProtectionDomain[] paramArrayOfProtectionDomain2)
  {
    Object localObject;
    Principal[] arrayOfPrincipal;
    int i = 0;
    if (paramArrayOfProtectionDomain1 != null)
      i = 0 + paramArrayOfProtectionDomain1.length;
    if (paramArrayOfProtectionDomain2 != null)
      i += paramArrayOfProtectionDomain2.length;
    if (i == 0)
    {
      localObject = null;
      return localObject;
    }
    ProtectionDomain[] arrayOfProtectionDomain = new ProtectionDomain[i];
    int j = 0;
    if (paramArrayOfProtectionDomain1 != null)
    {
      Set localSet = this.subject.getPrincipals();
      arrayOfPrincipal = (Principal[])localSet.toArray(new Principal[localSet.size()]);
      j = 0;
    }
    while (true)
    {
      while (j >= paramArrayOfProtectionDomain1.length)
      {
        if (paramArrayOfProtectionDomain2 != null)
          System.arraycopy(paramArrayOfProtectionDomain2, 0, arrayOfProtectionDomain, j, paramArrayOfProtectionDomain2.length);
        localObject = arrayOfProtectionDomain;
      }
      if (paramArrayOfProtectionDomain1[j] != null)
        arrayOfProtectionDomain[j] = new ProtectionDomain(paramArrayOfProtectionDomain1[j].getCodeSource(), paramArrayOfProtectionDomain1[j].getPermissions(), paramArrayOfProtectionDomain1[j].getClassLoader(), arrayOfPrincipal);
      ++j;
    }
  }

  public Subject getSubject()
  {
    SecurityManager localSecurityManager = System.getSecurityManager();
    if (localSecurityManager != null)
      localSecurityManager.checkPermission(_GET);
    return this.subject;
  }
}